﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Common;
using RabbitMQ.Client;


namespace Producer.Producers
{
    /// <summary>
    /// 工作队列模式
    /// </summary>
    internal class SmsSender
    {
        public static void Sender()
        {
            using (var connection = Common.RabbitUtils.GetConnention().CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(Common.RabbitConstant.QUEUE_SMS, true, false, false, null);
                    for (int i = 0; i < 1000; i++)
                    {
                        Sms sms = new Sms("乘客" + i, "139000" + i, "你的车票预定成功");
                        string jsonSms = JsonSerializer.Serialize(sms);
                        var body=Encoding.UTF8.GetBytes(jsonSms);
                        channel.BasicPublish("", Common.RabbitConstant.QUEUE_SMS, null, body);
                        Console.WriteLine($"正在发送内容：{jsonSms}");
                        //Thread.Sleep(1000);
                    }
                    Console.WriteLine("发送成功");
                }
            }
        }
    }
}
